﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomControl
{
	public class IfThenViewModel : INotifyPropertyChanged
	{
		ObservableCollection<IfStatement> _ifStatements;
		public ObservableCollection<IfStatement> IfStatements
		{
			get
			{
				return _ifStatements;
			}
			set
			{
				if (_ifStatements != value)
				{
					_ifStatements = value;
					NotifyOfPropertyChanged("IfStatements");
				}
			}
		}

		ObservableCollection<ThenStatement> _thenStatements;
		public ObservableCollection<ThenStatement> ThenStatements
		{
			get
			{
				return _thenStatements;
			}
			set
			{
				if (_thenStatements != value)
				{
					_thenStatements = value;
					NotifyOfPropertyChanged("ThenStatements");
				}
			}
		}

		IfStatement _selectedIfStatement;
		public IfStatement SelectedIfStatement
		{
			get
			{
				return _selectedIfStatement;
			}
			set
			{
				if (_selectedIfStatement != value)
				{
					_selectedIfStatement = value;
					NotifyOfPropertyChanged("SelectedIfStatement");
				}
			}
		}

		ThenStatement _selectedThenStatement;
		public ThenStatement SelectedThenStatement
		{
			get
			{
				return _selectedThenStatement;
			}
			set
			{
				if (_selectedThenStatement != value)
				{
					_selectedThenStatement = value;
					NotifyOfPropertyChanged("SelectedThenStatement");
				}
			}
		}

		public IfThenViewModel()
		{
			_ifStatements = new ObservableCollection<IfStatement>();
			_thenStatements = new ObservableCollection<ThenStatement>();
			CreateDefaultIfStatement();
			CreateDefaultThenStatements();
			SelectedIfStatement = IfStatements.Where(x => x.Name == "This Happens").FirstOrDefault();
			SelectedThenStatement = ThenStatements.Where(x => x.Name == "Do This").FirstOrDefault();
		}

		private void CreateDefaultIfStatement()
		{
			this.IfStatements.Add(new IfStatement() { Name = "This Happens", Order = 0 });
			this.IfStatements.Add(new IfStatement() { Name = "Create New", Order = 1 });
			this.IfStatements.Add(new IfStatement() { Name = "UDP Trigger", Order = 2 });
			this.IfStatements.Add(new IfStatement() { Name = "TCP Trigger", Order = 2 });
			this.IfStatements.Add(new IfStatement() { Name = "GPIO Trigger", Order = 2 });
			this.IfStatements.Add(new IfStatement() { Name = "Camera Trigger", Order = 2 });
		}

		private void CreateDefaultThenStatements()
		{
			this.ThenStatements.Add(new ThenStatement() { Name = "Do This" });
			this.ThenStatements.Add(new ThenStatement() { Name = "Create New" });
		}

		public event PropertyChangedEventHandler PropertyChanged;

		void NotifyOfPropertyChanged(string prop)
		{
			if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
		}

			
	}
}
